<template>
  <div class="pageSearch">
    <MyForm v-bind="formConfig" v-model="formData">
      <template #header>
        <h2>高级检索</h2>
      </template>
      <template #footer>
        <div class="btn">
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </template>
    </MyForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue"
import MyForm from "@/base-ui/form"

export default defineComponent({
  props: {
    formConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    MyForm
  },
  emits: ["reset", "search"],
  setup(props, { emit }) {
    // 双向绑定的属性应该由配置文件的field来决定
    const formItems = props.formConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ""
    }
    const formData = ref(formOriginData)

    // 重置
    const reset = () => {
      for (const key in formOriginData) {
        formData.value[key] = formOriginData[key]
      }
      emit("reset")
    }

    // 搜索
    const search = () => {
      console.log("点击了搜索")
      emit("search", formData.value)
    }
    return {
      formData,
      reset,
      search
    }
  }
})
</script>

<style scoped lang="less">
.btn {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
